package com.ygqh.baby.observer;

import com.ygqh.baby.service.wechat.model.good.GoodImportOrderRequestParam;
import com.ygqh.baby.service.wechat.service.WeixinGoodService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 观察者——微信好物圈推送订单
 *
 * @Author: sunshuo
 * @Date: 2019/5/22 14:23
 * @Version: 1.0
 */
@Component
public class WeixinGoodOrderObserver implements Observer {

	private final Logger logger = LoggerFactory.getLogger(InformObserver.class);
	private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(3);

	@Autowired
	private WeixinGoodService weixinGoodService;

	@Override
	public void update(Observable o, Object arg) {
		logger.info("WeixinGoodOrderObserver.update: {}", arg);
		OrderPayModel model = arg != null ? (OrderPayModel) arg : null;
		if (model == null || model.getOrderId() == null) {
			logger.info(o.getClass().getName() + ": orderCode 为空");
			return;
		}
		EXECUTOR_SERVICE.execute(() -> weixinGoodService.importOrder(model.getOrderId(), 0,
				GoodImportOrderRequestParam.ImportOrderAction.ADD_ORDER));
	}
}
